'use strict'

app = angular.module 'app'

app.directive 'btHas', ($state) ->
  restrict: 'A'
  scope: yes
  link: ($scope, el, attr) ->
    $ = angular.element
    input = el.find 'input'
    form = input.parent().parent()
    feedback_y = $('<span class="glyphicon glyphicon-ok form-control-feedback"></span>')
    feedback_n = $('<span class="glyphicon glyphicon-remove form-control-feedback"></span>')
    input.after feedback_y.addClass 'ng-hide'
    input.after feedback_n.addClass 'ng-hide'
    debugger
    $scope.$watch () ->
      [input.hasClass('ng-dirty'), form.hasClass('ng-dirty'), input.hasClass('ng-invalid')].join(',')
    , (val) ->

      dirty = input.hasClass('ng-dirty') or form.hasClass('ng-dirty')
      invalid = input.hasClass 'ng-invalid'
      return if not dirty

      el.addClass 'has-feedback'

      if invalid
        el.removeClass 'has-success'
        el.addClass 'has-error'
        feedback_n.removeClass 'ng-hide'
        feedback_y.addClass 'ng-hide'
      else
        el.removeClass 'has-error'
        el.addClass 'has-success'
        feedback_y.removeClass 'ng-hide'
        feedback_n.addClass 'ng-hide'



